package com.immediatus.logic;

import android.graphics.Color;
import com.immediatus.R;
import com.immediatus.engine.contracts.IGestureDetector;
import com.immediatus.engine.contracts.IGestureListener;
import com.immediatus.engine.contracts.IUnit;
import com.immediatus.engine.image.ImageLabel;
import com.immediatus.engine.image.MultiImageLabel;
import com.immediatus.engine.input.GestureDetector;
import com.immediatus.engine.input.TouchEvent;
import com.immediatus.engine.interpolator.AccelerationInterpolator;
import com.immediatus.engine.layer.Canvas;
import com.immediatus.engine.modifier.DoubleValueSpanUnitModifier;
import com.immediatus.engine.modifier.NullUnitModifier;
import com.immediatus.engine.text.TextLabel;
import com.immediatus.graphics.contracts.IBitmapTextureAtlasSource;
import com.immediatus.graphics.factories.FontFactory;
import com.immediatus.graphics.factories.TextureRegionFactory;
import com.immediatus.graphics.font.Font;
import com.immediatus.graphics.texture.TextureOptions;
import com.immediatus.graphics.texture.bitmap.AssetBitmapTextureAtlasSource;
import com.immediatus.graphics.texture.bitmap.BitmapTextureAtlas;
import com.immediatus.graphics.texture.region.TabledTextureRegion;
import com.immediatus.graphics.texture.region.TextureRegion;
import com.immediatus.ui.BaseActivity;

public class MainScreen{

    private BitmapTextureAtlas _mainscreentextureatlas;
    private TextureRegion _moonTextureRegion;
    private TextureRegion _grassTextureRegion;
    private TabledTextureRegion _buttonTextureRegion;
    private Font _font;

    private BaseActivity _activity;

    public MainScreen(BaseActivity activity_){
        _activity = activity_;
    }

    public void loadResources() {
        _mainscreentextureatlas = new BitmapTextureAtlas(1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        _activity.getEngine().getTextureManager().loadTexture(_mainscreentextureatlas);

        IBitmapTextureAtlasSource grassTextureSource = new AssetBitmapTextureAtlasSource(_activity, "gfx/grass.png");
        IBitmapTextureAtlasSource moonTextureSource = new AssetBitmapTextureAtlasSource(_activity, "gfx/moon.png");
        IBitmapTextureAtlasSource bitmapTextureAtlasSource = new AssetBitmapTextureAtlasSource(_activity, "gfx/button.png");



        this._moonTextureRegion = TextureRegionFactory.createFromSource(_mainscreentextureatlas, moonTextureSource, 0, 0, false);
        this._grassTextureRegion = TextureRegionFactory.createFromSource(_mainscreentextureatlas, grassTextureSource, 0, 425, false);
        this._buttonTextureRegion = TextureRegionFactory.createTabledFromSource(_mainscreentextureatlas, bitmapTextureAtlasSource, 0, 425 + 215, 3, 3, false);


        BitmapTextureAtlas fontTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        FontFactory.setAssetBasePath("font/");
        this._font = FontFactory.createFromAsset(fontTexture, _activity, "kelly.ttf", 60, true, Color.WHITE);
        _activity.getEngine().getTextureManager().loadTexture(fontTexture);
        _activity.getFontManager().loadFont(this._font);

    }

    public void  initLayer(final Canvas canvas_) {
        final ImageLabel grass = new ImageLabel(0, 800 - 215,  _grassTextureRegion);
        final ImageLabel moon = new ImageLabel(0, 900 - 425,  _moonTextureRegion);
        final MultiImageLabel button = new MultiImageLabel(0, 0, _buttonTextureRegion);
        final IGestureDetector detector = new GestureDetector(new IGestureListener(){
            @Override
            public boolean touchDown(int x_, int y_, int pointer_){
                return true;
            }

            @Override
            public boolean tap(int x_, int y_, int count_){
                return true;
            }

            @Override
            public boolean longPress(int x_, int y_){
                return true;
            }

            @Override
            public boolean fling(float velocityX_, float velocityY_){
                return true;
            }

            @Override
            public boolean pan(int x_, int y_, int deltaX_, int deltaY_){
                return true;
            }

            @Override
            public boolean zoom(float originalDistance_, float currentDistance_){
                return true;
            }
        });


        final TextLabel logoText = new TextLabel(0, 10, this._font, _activity.getString(R.string.app_name)) {
            @Override
            public boolean onAreaTouched(TouchEvent event_, float x_, float y_){
                  return detector.onTouch(event_.getMotionEvent());
//                this.setVisible(false);
//                moon.registerEntityModifier(new DoubleValueChangeUnitModifier(.5F, 650, 0.4F, new AccelerationInterpolator(0.3F, 0.7F)) {
//                    private final float _y = moon.getY();
//                    private final float _s = moon.getScaleX();
//
//                    @Override
//                    protected void onChangeValues(IUnit item_, float secondsElapsed_, float valueA_, float valueB_){
//                        item_.setPosition(item_.getX(), _y - valueA_);
//                        item_.setScale(_s + valueB_);
//                    }
//
//                    @Override
//                    public NullUnitModifier deepCopy(){
//                        return new NullUnitModifier();
//                    }
//                });
//                grass.registerEntityModifier(new SingleValueChangeUnitModifier(.5F, 580, new AccelerationInterpolator(0.3F, 0.7F)) {
//                    private final float _y = grass.getY();
//
//                    @Override
//                    protected void onChangeValue(IUnit item_, float secondsElapsed_, float value_){
//                        item_.setPosition(item_.getX(), _y - value_);
//                    }
//
//                    @Override
//                    public NullUnitModifier deepCopy(){
//                        return new NullUnitModifier();
//                    }
//                });
//                return true;
            }
        };

        moon.registerEntityModifier(new MoonAnimator());
        moon.setScale(.5F);
        moon.setScaleCenter(238, 212);


        canvas_.attachChild(moon);
        canvas_.attachChild(grass);
        canvas_.attachChild(logoText);
        canvas_.attachChild(button);
        canvas_.registerTouchArea(logoText);

    }

    public void resetLayer(final Canvas canvas_) {
    }

    class MoonAnimator extends DoubleValueSpanUnitModifier {
        float _y = 900 - 425;

        MoonAnimator(){
            super(1.F, 0, 100, .5F, 1.F, new AccelerationInterpolator(0.3F, 0.7F));
        }

        @Override
        protected void onSetInitialValues(IUnit item_, float valueA_, float valueB_){
            item_.setScale(valueB_);
        }

        @Override
        protected void onChangeValues(IUnit item_, float secondsElapsed_, float valueA_, float valueB_){
            item_.setPosition(item_.getX(), _y - valueA_);
            item_.setScale(valueB_);
        }

        @Override
        public NullUnitModifier deepCopy(){
            return new NullUnitModifier();
        }
    }
}
